package org.zhangchao.poi.excel.create;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.zhangchao.poi.excel.common.template.ExcelTemplateParam;
import org.zhangchao.poi.excel.interceptor.ExcelHandlerInterceptor;
import org.zhangchao.poi.excel.interceptor.ExcelHandlerInterceptorStack;
import org.zhangchao.poi.excel.interceptor.MakeHandler;
import org.zhangchao.poi.excel.operate.ExcelTemplate;

/**
 * 
 * @author zhangchao
 *
 */
public class ExcelBaseCreate<E> extends ExcelCreate implements MakeHandler {

	/** 拦截器栈 */
	private ExcelHandlerInterceptorStack handlerStack;
	
	/** Excel模板 */
	private ExcelTemplate excelTemplate;

	/** 数据集合 */
	private List<E> data = new ArrayList<E>();
	
	/** 模板标注数据集合  */
	private Map<String, String> labelMap = new HashMap<String, String>();
	
	/**
	 * 
	 */
	public ExcelBaseCreate() {
		super();
	}
	
	/**
	 * -1, > 0
	 * @param i -1, > 0
	 */
	public ExcelBaseCreate(int i) {
		super(i);
	}
	
	/**
	 * 
	 * @param templateFile
	 *//*
	public ExcelBaseCreate(String fileName) {
		super();
		this.excelTemplate = new ExcelTemplate(fileName);
	}*/
	
	/**
	 * 
	 * @param templateFile
	 */
	public ExcelBaseCreate(File templateFile) {
		super();
		this.excelTemplate = new ExcelTemplate(templateFile);
	}
	
	/**
	 * 
	 * @param templateInputStream
	 */
	public ExcelBaseCreate(InputStream templateInputStream) {
		super();
		this.excelTemplate = new ExcelTemplate(templateInputStream);
	}
	
	/**
	 * 
	 * @param i
	 * @param templateFile
	 */
	public ExcelBaseCreate(int i, File templateFile) {
		super(i);
		this.excelTemplate = new ExcelTemplate(templateFile);
	}
	
	/**
	 * 
	 * @param i
	 * @param templateInputStream
	 */
	public ExcelBaseCreate(int i, InputStream templateInputStream) {
		super(i);
		this.excelTemplate = new ExcelTemplate(templateInputStream);
	}
	
	@Override
	public void createRow(Row row) {
		if (null == handlerStack) {
			return;
		}
		handlerStack.handler(row, this, handlerStack);		
	}
	
	@Override
	public void create() {
		//如果模板不为空
		if(null != excelTemplate) {
			excelTemplate.create(this);
		}else {
			if(null != data) {
				for (int i = 0; i <= data.size(); i++) {
					createRow(i);
				}
			}
		}
	}
	
	@Override
	public Cell copyCell(Row row, int i, Cell cell) {
		Cell newcell = super.copyCell(row, i, cell);
		if(Cell.CELL_TYPE_STRING == newcell.getCellType()) {
			String cellValue = cell.getStringCellValue().trim();
			if (cellValue.startsWith("#")) {
				String key = cellValue.substring(1);
				if (labelMap.containsKey(key)) {
					newcell.setCellValue(labelMap.get(key));
				}
			}
		}
		return newcell;
	}
	
	@Override
	public List<?> prepareModelData() {
		return getData();
	}
	
	@Override
	public ExcelTemplateParam getExcelTemplateaParam() {
		if(null == excelTemplate) {
			return null;
		}
		ExcelTemplateParam tempEtp = excelTemplate.getExcelTemplateaParam();
		ExcelTemplateParam etp = new ExcelTemplateParam();
		etp.setInitColIndex(tempEtp.getInitColIndex());
		etp.setRowHeight(tempEtp.getRowHeight());
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.cloneStyleFrom(tempEtp.getDefaultStyle());
		etp.setDefaultStyle(cellStyle);
		Map<Integer, CellStyle> cellStyles = new HashMap<Integer, CellStyle>();
		etp.setStyles(cellStyles);
		for(Entry<Integer, CellStyle> entry : tempEtp.getStyles().entrySet()) {
			Integer key = entry.getKey();
			CellStyle cs = workbook.createCellStyle();
			cs.cloneStyleFrom(entry.getValue());
			cellStyles.put(key, cs);
		}
		return etp;
	}
	
	/**
	 * 添加拦截器
	 * @param excelHandlerInterceptor
	 */
	public void addExcelHandleerInterceptor(
			ExcelHandlerInterceptor excelHandlerInterceptor) {
		handlerStack.add(excelHandlerInterceptor);
	}

	public ExcelHandlerInterceptorStack getHandlerStack() {
		return handlerStack;
	}

	public void setHandlerStack(ExcelHandlerInterceptorStack handlerStack) {
		this.handlerStack = handlerStack;
	}

	public ExcelTemplate getExcelTemplate() {
		return excelTemplate;
	}

	public void setExcelTemplate(ExcelTemplate excelTemplate) {
		this.excelTemplate = excelTemplate;
	}

	public List<E> getData() {
		return data;
	}

	public void setData(List<E> data) {
		this.data = data;
	}
	
	public Map<String, String> getLabelMap() {
		return labelMap;
	}

	public void setLabelMap(Map<String, String> labelMap) {
		this.labelMap = labelMap;
	}
	
}
